import Sequelize from 'sequelize'
import sequelize from './../../../database'

export const CustomerUnit = sequelize.define('customer_unit',{
    id:{
        type: Sequelize.UUIDV4,
        defaultValue: Sequelize.UUIDV4,
        unique: true,
        allowNull: false,
        primaryKey:true,
    },
    customer_group_id: {
        type: Sequelize.STRING,
        allowNull: false,
    },
    customer_unit_name:{
        type: Sequelize.STRING,
        allowNull: false
    },
    contact_name: {
        type: Sequelize.STRING,
        allowNull: false
    },
    contact_tel: {
        type: Sequelize.STRING,
        allowNull: false
    },
    supervise_tel:{
        type: Sequelize.STRING,
        allowNull: false
    },
    shortv_tel:{
        type: Sequelize.STRING,
        allowNull: false
    },
    pinyin_name:{
        type: Sequelize.STRING
    },
    memo: {
        type: Sequelize.STRING
    },
    status:{
        type:Sequelize.INTEGER,
        defaultValue: 0,
        allowNull: false
    },
    created_man: {
        type: Sequelize.STRING,
        allowNull: false
    }
},{
    freezeTableName:false,
    tableName: 'customer_unit',
    updatedAt:'updated_at',
    createdAt:'created_at'
});
class CustomerUnitModel{
    insert(record){
        return CustomerUnit.create(record);
    }
    update(record){
        return CustomerUnit.update(
            {
                'where':{
                    'id': record.id
                }
            }
        )
    }
    delete(id){
        return CustomerUnit.destroy({id: id});
    }
    findById(id){
        return sequelize.query(`
            select cu.ID id, cu.customer_group_id, cu.customer_unit_name, cu.contact_name
            from customer_unit cu, customer_group cg
            where cu.id = '${id}' and cg.id = cu.customer_group_id`, { type: sequelize.QueryTypes.SELECT });
    }
    findAll(){
        return CustomerUnit.findAll();
    }
    findByPage(record){
        return CustomerUnit.findAndCount({
            limit:record.page_size,
            offset: record.page_no
        })
    }
}
module.exports = new CustomerUnitModel();